package com.wmlt.api;

import com.alibaba.fastjson.JSONObject;
import com.wmlt.entity.Message;
import com.wmlt.entity.MessageResponseData;
import com.wmlt.service.MessageService;
import com.wmlt.service.impl.MessageServiceImpl;
import com.wmlt.util.GetRequestJsonUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;

/**
 * @author liyuxiang
 * @description: 私信api
 * @param:
 * @return:
 * @date: 2021/4/10 11:41 上午
 */
public class ChatApi extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        resp.setContentType("text/json;charset=utf-8");
        switch (path) {
            case "/chat/sendMessage":
                sendMessage(req, resp);
                break;
            case "/chat/getMessageListByUserId":
                getMessageListByUserId(req, resp);
                break;
            case "/chat/getMessageRecord":
                getMessageRecord(req, resp);
                break;
            default:
                resp.setStatus(404);
                break;
        }
    }

    /**
     * @description: 发送消息
     * @param:
     * @return:
     * @author liyuxiang
     * @date: 2021/4/10 11:43 上午
     */
    private void sendMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        JSONObject jsonObject = GetRequestJsonUtils.getRequestJsonObject(response, request);
        String senderUserId = jsonObject.getString("senderUserId");
        String addresseeUserId = jsonObject.getString("addresseeUserId");
        String messageStr = jsonObject.getString("message");
        String senderTime = jsonObject.getString("senderTime");
        Message message = new Message(senderUserId, addresseeUserId, messageStr, senderTime, 0);
        System.out.println(message);
        MessageService messageService = new MessageServiceImpl();
        int result = messageService.sendMessage(message);
        System.out.println("返回结果" + result);
        MessageResponseData messageResponseData = new MessageResponseData();
        messageResponseData.setCode(result > 0 ? 200 : 400);
        messageResponseData.setSuccess(result > 0 ? "发送成功" : "发送失败");
        String jsonStr = JSONObject.toJSONString(messageResponseData);
        System.out.println(jsonStr);
        response.getWriter().println(jsonStr);
    }

    /**
     * @description: 获取消息列表
     * @param:
     * @return:
     * @author liyuxiang
     * @date: 2021/4/10 2:41 下午
     */
//    select distinct use.username,use.user_img,use.user_id, from `message`  as `msg` , `User`  as `use`  where msg.addressee_user_id=use.user_id and msg.sender_user_id=1
    private void getMessageListByUserId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String senderUserId=request.getParameter("senderUserId");//发送人id
        String addresseeUserId=request.getParameter("addresseeUserId");//接收人id
        MessageService messageService = new MessageServiceImpl();
        List<Message> messageList = messageService.getMessageListByUserId(senderUserId,addresseeUserId);
        System.out.println(messageList.size());
        for (int i = 0; i < messageList.size(); i++) {
            if (messageList.get(i).getUser().getUserId().equals(senderUserId)){
                messageList.remove(i);
            }
        }
        System.out.println(messageList.size());

        String jsonStr = JSONObject.toJSONString(messageList);
        System.out.println(jsonStr);
        response.getWriter().println(jsonStr);
    }
    /**
     * @description: 获取消息记录
     * @param:
     * @return:
     * @author liyuxiang
     * @date: 2021/4/12 10:51 上午
     */
    private void getMessageRecord(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String senderUserId=request.getParameter("senderUserId");//发送人id
        String addresseeUserId=request.getParameter("addresseeUserId");//接收人id
        System.out.println("发送人ID"+senderUserId);
        System.out.println("接收人ID"+addresseeUserId);
        MessageService messageService = new MessageServiceImpl();
        List<Message> messageList = messageService.getMessageRecord(senderUserId,addresseeUserId);
        messageList.sort(Comparator.comparing(Message::getSenderTime));
        for (Message message : messageList) {
            System.out.println(message);
        }
        String jsonStr = JSONObject.toJSONString(messageList);
        System.out.println(jsonStr);
        response.getWriter().println(jsonStr);
    }

}